home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 28
/
Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso
/
Aminet
/
game
/
board
/
Crafty-15.19.lha
/
crafty-15.19
/
src
/
time.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-09-13
|
15KB
|
339 lines
#include <stdio.h>
#include <stdlib.h>
#include "chess.h"
#include "data.h"
/* last modified 09/27/96 */
/*
********************************************************************************
* *
* TimeAdjust() is called to adjust timing variables after each program move *
* is made. it simply increments the number of moves made, decrements the *
* amount of time used, and then makes any necessary adjustments based on the *
* time controls. *
* next search. *
* *
********************************************************************************
*/
void TimeAdjust(int time_used, PLAYER player)
{
/*
----------------------------------------------------------
| |
| decrement the number of moves remaining to the next |
| time control. then subtract the time the program took |
| to choose its move from the time remaining. |
| |
----------------------------------------------------------
*/
if (player == crafty) {
tc_moves_remaining--;
tc_time_remaining-=(tc_time_remaining > time_used) ?
time_used : tc_time_remaining;
if (!tc_moves_remaining) {
if (tc_sudden_death == 2) tc_sudden_death=1;
tc_moves_remaining+=tc_secondary_moves;
tc_time_remaining+=tc_secondary_time;
tc_time_remaining_opponent+=tc_secondary_time;
Print(4095,"time control reached\n");
}
if (tc_increment) tc_time_remaining+=tc_increment;
}
else {
tc_time_remaining_opponent-=(tc_time_remaining_opponent > time_used) ?
time_used : tc_time_remaining_opponent;
if (tc_increment) tc_time_remaining_opponent+=tc_increment;
}
}
/* last modified 06/15/98 */
/*
********************************************************************************
* *
* TimeCheck() is used to determine when the search should stop. it uses *
* several conditions to make this determination: (1) the search time has *
* exceeded the time per move limit; (2) the value at the root of the tree *
* has not dropped to low. (3) if the root move was flagged as "easy" and *
* no move has replaced it as best, the search can actually be stopped early *
* to save some time on the clock. if (2) is true, then the time is extended *
* based on how far the root value has dropped in an effort to avoid whatever *
* is being lost. *
* *
********************************************************************************
*/
int TimeCheck(int abort)
{
int time_used;
int value, last_value, searched;
int *i, ndone;
TREE *tree=local[0];
/*
----------------------------------------------------------
| |
| first, check to see if we are searching the first move |
| at this depth. if so, and we run out of time, we can |
| abort the search rather than waiting to complete this |
| ply=1 move to see if it's better. |
| |
----------------------------------------------------------
*/
if (tree->last[0]==(tree->last[1]-1) && !booking && !annotate_mode &&
!pondering && iteration_depth>4) return(1);
ndone=0;
for (i=tree->last[0];i<tree->last[1];i++)
if (tree->searched_this_root_move[i-tree->last[0]]) ndone++;
if (ndone == 1) abort=1;
if (iteration_depth <= 2) return(0);
/*
----------------------------------------------------------
| |
| now, check to see if we need to "burp" the time to |
| let the operator know the search is progressing and |
| how much time has been used so far. |
| |
----------------------------------------------------------
*/
time_used=(ReadClock(time_type)-start_time);
if (tree->nodes_searched>noise_level && (display_options&32) && time_used>burp) {
#if defined(SMP)
Lock(lock_io);
#endif
#if defined(MACOS)
printf(" %2i %s\n",iteration_depth,DisplayTime(time_used));
#else
printf(" %2i %s\r",iteration_depth,DisplayTime(time_used));
#endif
burp=(time_used/1500)*1500+1500;
fflush(stdout);
#if defined(SMP)
UnLock(lock_io);
#endif
}
if (pondering || analyze_mode) return(0);
if (time_used > absolute_time_limit) return(1);
if (easy_move && !search_time_limit) {
if (time_limit>100 && time_used<time_limit/3) return (0);
}
else if (time_used < time_limit) return(0);
if (search_time_limit) return(1);
/*
----------------------------------------------------------
| |
| ok. we have used "time_limit" at this point. now the |
| question is, can we stop the search? |
| |
| first, make sure that we have actually found a score |
| at the root of the tree. if not, we can safely stop |
| searching without wasting any more time. |
| |
----------------------------------------------------------
*/
if ((root_value == root_alpha) && !search_failed_low) {
searched=0;
for (i=tree->last[0];i<tree->last[1];i++)
if (tree->searched_this_root_move[i-tree->last[0]]) searched++;
if (searched == 1) return(1);
}
/*
----------------------------------------------------------
| |
| we have a score at the root of the tree, if the |
| evaluation is not significantly worse than the last |
| evaluation (from the previous iteration...) then we |
| safely stop the search. note also that if the current |
| evaluation is quite a bit worse, but we are still way |
| ahead, we can still avoid using extra time. |
| |
----------------------------------------------------------
*/
value=root_value;
last_value=last_search_value;
if ((value>=last_value-33 && !search_failed_low) ||
(value>350 && value >= last_value-67)) {
if (time_used > time_limit*2) return(1);
else return(abort);
}
/*
----------------------------------------------------------
| |
| we are in trouble at the root. depending on how much |
| the score has dropped, increase the search time limit |
| to try and correct the problem. for a positional drop |
| we can double the search time (this is for a serious |
| drop, of course). |
| |
----------------------------------------------------------
*/
if (time_used < time_limit*2.5 && time_used+500<tc_time_remaining) return(0);
if ((value >= last_value-67 && !search_failed_low) ||
value>550) return(abort);
/*
----------------------------------------------------------
| |
| we are in really serious trouble at the root, losing |
| material. increase the time limit to 6X the original |
| target, as losing material is tantamount to losing the |
| game anyway. |
| |
----------------------------------------------------------
*/
if (time_used < time_limit*6 && time_used+500<tc_time_remaining) return(0);
return(1);
}
/* last modified 10/17/97 */
/*
**********